<template>
  <router-view></router-view>
  <van-tabbar route v-model="active" @change="onChange">
    <van-tabbar-item icon="home-o" to="/home">首页</van-tabbar-item>
    <van-tabbar-item icon="cart-o" to="/cart">购物车</van-tabbar-item>
    <van-tabbar-item icon="user-o" to="/my">我的</van-tabbar-item>
  </van-tabbar>
</template>

<script>
import { ref, getCurrentInstance } from 'vue'
// import router from './router';
export default {
  setup() {
    const active = ref(0)
    const { proxy } = getCurrentInstance()

     console.log(proxy);
    const onChange = (index) => {
      const tabMap = {
        cart: 1,
        my: 2
      }
      if (index === tabMap.cart || index === tabMap.my) {
        if (localStorage.getItem('isLogin')) {
          proxy.$router.push(index === tabMap.cart ? '/cart' : '/my')
        } else {
          proxy.$toast('请先登录')
          setTimeout(() => {
            proxy.$router.push('/login')
          }, 500)
        }
      }
      // console.log('index',index);
      // if(index == 1){
      //   //是否登录
      //   if (localStorage.getItem('isLogin')){
      //     router.push('/cart')
      //   }else{
      //     proxy.$toast('请先登录');
      //     setTimeout(()=>{
      //       router.push('/login')
      //     },500)
      //   }
      // }
      // if(index == 2){
      //   //是否登录
      //   if (localStorage.getItem('isLogin')){
      //     router.push('/my')
      //   }else{
      //     proxy.$toast('请先登录');
      //     setTimeout(()=>{
      //       router.push('/login')
      //     },500)
      //   }
      // }
    }
    return {
      active,
      onChange
    }
  }
}
</script>